﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DTO;
using System.Data;

namespace DAO
{
    public class TrangChuDAO
    {
        DataProvider provider;

        public TrangChuDAO()
        {
            provider = new DataProvider();
        }

        public int ThemTrangChu(TrangChuDTO trangChu)
        {
            string sqlStatement = "INSERT INTO TrangChu(NoiDung) VALUES (N'" + trangChu.NoiDung + "')";

            return provider.ExecuteNonQuery(sqlStatement);
        }
        public List<TrangChuDTO> LayDanhSach()
        {
            List<TrangChuDTO> arrTr = new List<TrangChuDTO>();
            try
            {
                string query = "SELECT * FROM TrangChu";
                DataTable dt = new DataTable();
                dt = provider.ExecuteQuery(query);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TrangChuDTO item = new TrangChuDTO();
                    item.MaNoiDung = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    item.NoiDung = dt.Rows[i].ItemArray[1].ToString();
                    arrTr.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
            return arrTr;
        }
        public int CapNhatTrangChu(TrangChuDTO trangChu)
        {
            string sqlStatement = "UPDATE TrangChu SET NoiDung=N'" + trangChu.NoiDung + "' WHERE MaNoiDung=" + trangChu.MaNoiDung;

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int XoaTrangChu(TrangChuDTO trangChu)
        {
            string sqlStatement = "DELETE * FROM TrangChu WHERE MaNoiDung=" + trangChu.MaNoiDung;

            return provider.ExecuteNonQuery(sqlStatement);
        }
    }
}
